import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/login.vue'
import Home from './components/home.vue'
import Welcome from './components/welcome'
import Users from './components/user/users'
Vue.use(Router)

const router = new Router({
        routes: [{
                path: '/',
                redirect: '/login'
            }, {
                path: '/login',
                name: 'login',
                component: Login
            }, {
                path: '/home',
                name: 'home',
                redirect: '/welcome',
                component: Home,
                children: [{
                        path: '/welcome',
                        name: 'welcome',
                        component: Welcome,
                    },
                    {
                        path: '/users',
                        name: 'users',
                        component: Users,
                    },
                ]
            }

        ]
    })
    //路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问得路径
    //from 代表从那个路径跳转而来
    // next() 方法  强制跳转
    if (to.path === '/login') return next()
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
    next()
})

export default router